package com.example.movie.ui.user

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.data.user.login.model.SharedLoginViewModel
import com.example.movie.ui.MainActivity
import com.example.movie.ui.user.adapter.SectionsPagerAdapter
import com.example.movie.utils.ContextHolder
import com.google.android.material.appbar.AppBarLayout
import kotlinx.android.synthetic.main.fragment_movie_detail.*
import kotlinx.android.synthetic.main.fragment_person_page.*
import kotlinx.android.synthetic.main.fragment_person_page.toolbar

class PersonPageActivity : AppCompatActivity() {

    private val loginViewModel: SharedLoginViewModel =
        ContextHolder.from(MainActivity::class).loginViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_person_page)
        setObservers()
        initView()
    }

    private fun initView() {
        view_pager.adapter = SectionsPagerAdapter(this, supportFragmentManager)
        tabs.setupWithViewPager(view_pager)

        app_bar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _: AppBarLayout, verticalOffset: Int ->
            if (verticalOffset == 0) {
                activity_title.visibility = View.GONE
                toolbar.setBackgroundResource(R.color.colorPrimary)
                toolbar.setNavigationIcon(R.drawable.back_white)
            } else {
                Log.e("dev", "initView: $verticalOffset", )
                activity_title.visibility = View.VISIBLE
                toolbar.setNavigationIcon(R.drawable.back_red)
                toolbar.setBackgroundResource(R.color.white)
            }
        })

        toolbar.setNavigationOnClickListener {
            onBackPressed()
        }

        edit.setOnClickListener {
            startActivity(Intent(this, UserInfoEditActivity::class.java))
        }

        intent?.extras?.getInt(AppConstants.SELECT_TAB)?.let {
            tabs.getTabAt(it)?.select()
        }

//        fab.setOnClickListener { view ->
//            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                .setAction("Action", null).show()
//        }

    }


    private fun setObservers() {
        loginViewModel.loginResult.observe(this, { it ->
            it?.success?.let {
                with(it) {
                    username.text = nickName
                    sign_text.text = sign

                    Glide.with(this@PersonPageActivity)
                        .load(AppConstants.STATIC_SOURCE_URL + headImg)
                        .placeholder(R.mipmap.logo)
                        .error(R.drawable.error)
                        .into(punish_person_head)
                }
            }

        })

    }


}